#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# https://blog.csdn.net/github_39655029/article/details/80144073
# 绘制时钟

import turtle
import datetime


# 当前日期属于一周的第几天
def Week(t):
    week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
    return week[t.weekday()]


# 获取当前时间
def Date(t):
    y = t.year
    m = t.month
    d = t.day
    cur_hour = t.hour
    cur_min = t.minute
    cur_sec = t.second
    return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec)


# 移动画笔,距离为distance
def movePen(printer, distance):
    printer.penup()
    printer.pensize(5)
    printer.pencolor("blue")
    printer.fd(distance)
    printer.pendown()


# 绘制表针
def makeHands(window, printer, name, length):
    # 清空窗口，重置turtule状态为初始状态
    printer.reset()
    movePen(printer, -length * 0.1)
    # 开始记录多边形的顶点
    printer.begin_poly()
    printer.fd(length * 1.1)
    # 停止记录多边形的顶点
    printer.end_poly()
    # 返回记录的多边形
    handForm = printer.get_poly()
    window.register_shape(name, handForm)


# 初始化
def initial(window, printer):
    global secHand, minHand, hurHand

    # 隐藏画笔
    printer.hideturtle()
    printer.penup()

    # 重置方向向北(上)，正角度为顺时针
    window.mode("logo")
    # 建立并初始化表针
    makeHands(window, printer, "secHand", 180)
    makeHands(window, printer, "minHand", 150)
    makeHands(window, printer, "hurHand", 110)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    minHand = turtle.Turtle()
    minHand.shape("minHand")
    hurHand = turtle.Turtle()
    hurHand.shape("hurHand")

    for hand in secHand, minHand, hurHand:
        hand.shapesize(1, 1, 4)
        hand.speed(0)


# 绘制表盘外框
def drawClock(window, printer, R):
    # 清空窗口，重置turtule状态为初始状态
    window.reset()
    # 画笔尺寸
    printer.pensize(5)
    for i in range(60):
        movePen(printer, R)
        if i % 5 == 0:
            printer.fd(20)
            movePen(printer, -R - 20)

            movePen(printer, R + 20)
            if i == 0:
                # 写文本
                printer.write(int(12), align="center", font=("Consolas", 14, "bold"))
            elif i == 30:
                movePen(printer, 25)
                printer.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
                movePen(printer, -25)
            elif (i == 25 or i == 35):
                movePen(printer, 20)
                printer.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
                movePen(printer, -20)
            else:
                printer.write(int(i / 5), align="center", font=("Consolas", 14, "bold"))
            movePen(printer, -R - 20)
        else:
            # 绘制指定半径和颜色的点
            printer.dot(5, "red")
            movePen(printer, -R)
        printer.right(6)


# 表针的动态显示
def handsMove():
    t = datetime.datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
    secHand.seth(6 * second)
    minHand.seth(6 * minute)
    hurHand.seth(30 * hour)

    window.tracer(False)
    printer.fd(65)
    printer.pencolor("green")
    printer.write(Week(t), align="center", font=("黑体", 14))
    printer.back(130)
    printer.write(Date(t), align="center", font=("Consolas", 14))
    # 设置当前画笔位置为原点，方向朝东
    printer.home()
    window.tracer(True)

    # 经过100ms后继续调用handsMove函数
    window.ontimer(handsMove, 100)


def main():
    global window, printer

    window = turtle.Screen()  # creat a screen
    printer = turtle.Turtle()  # 输出文字

    # 调用定义的函数，打开和关闭动画，为更新图纸设置延迟；
    window.tracer(False)
    initial(window, printer)
    drawClock(window, printer, 200)
    window.tracer(True)
    handsMove()
    window.mainloop()


if __name__ == "__main__":
    main()
